﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RenShiKaoQin.Account
{
    public partial class StudyForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (hrdbEntities ent = new hrdbEntities())
                {
                    var result = from kqemp in ent.TKQEmployees where kqemp.FEmployeeID.Equals(Page.User.Identity.Name) select kqemp;
                    if (result.Any())
                    {
                        TKQEmployee kq = result.Single();
                        if (!kq.IsStudyManager)
                        {
                            Response.Write("您无权查看此页面");
                            Response.End();
                        }

                    }
                    else
                    {
                        Response.Write("您无权查看此页面");
                        Response.End();
                    }
                    DateTime now = DateTime.Now;
                    DateTime monthS = now.AddDays(-(now.Day - 1));
                    tbStart.Text = monthS.ToString("yyyy-MM-dd");
                    tbEnd.Text = monthS.AddDays(DateTime.DaysInMonth(now.Year, now.Month) - 1).ToString("yyyy-MM-dd");
                    btnSearch_Click(null, null);
                }
            }
            tbStart.Attributes["readonly"] = "readonly";
            tbEnd.Attributes["readonly"] = "readonly";
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            sqlSP.SelectParameters.Clear();
            sqlSP.SelectParameters.Add("CheckStatus", TypeCode.Boolean, cbDisplaySP.Checked.ToString());
            if (tbName.Text != "")
            {
                sqlSP.SelectParameters.Add("EmployeeName", tbName.Text);
                string tmin = DateTime.Now.AddDays(2).ToString("u");
                string tmax = DateTime.Now.AddDays(1).ToString("u");
                sqlSP.SelectParameters.Add("StartDate", TypeCode.DateTime, tmin.Substring(0, tmin.Length - 1));
                sqlSP.SelectParameters.Add("EndDate", TypeCode.DateTime, tmax.Substring(0, tmax.Length - 1));
            }
            else
            {
                sqlSP.SelectParameters.Add("EmployeeName", string.Empty);
                sqlSP.SelectParameters["EmployeeName"].ConvertEmptyStringToNull = false;
                sqlSP.SelectParameters.Add("StartDate", TypeCode.DateTime, tbStart.Text);
                sqlSP.SelectParameters.Add("EndDate", TypeCode.DateTime, tbEnd.Text);
            }
        }
        protected void cbDisplaySP_CheckedChanged(object sender, EventArgs e)
        {
            btnSearch_Click(null, null);
        }
        protected void gvSP_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int serial = Convert.ToInt32(e.Keys[0]);
            sqlSP.UpdateParameters["CheckStatus"] = new Parameter("CheckStatus", TypeCode.Boolean, "True");
        }
        protected void gvSP_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DispReason" || e.CommandName == "UpdateReason")
            {
                int rowIndex = int.Parse(e.CommandArgument.ToString());
                TextBox tbReason = (TextBox)gvSP.Rows[rowIndex].FindControl("tbReason");
                LinkButton lkbUpdate = (LinkButton)gvSP.Rows[rowIndex].FindControl("lkbUpdate");
                LinkButton lkbDispReason = (LinkButton)gvSP.Rows[rowIndex].FindControl("lkbDispReason");
                if (e.CommandName == "DispReason")
                {
                    tbReason.Visible = true;
                    lkbUpdate.Visible = true;
                    lkbDispReason.Visible = false;
                }
                if (e.CommandName == "UpdateReason")
                {
                    using (hrdbEntities ent = new hrdbEntities())
                    {
                        int serial = Convert.ToInt32(gvSP.DataKeys[rowIndex].Value);
                        var rrecord = from trec in ent.TKQStudyRequests where trec.SerialNO == serial select trec;
                        TKQStudyRequest rec = rrecord.Single();
                        rec.RejectStatus = true;
                        rec.RejectReason = tbReason.Text;
                        ent.SaveChanges();
                    }
                    tbReason.Visible = false;
                    lkbUpdate.Visible = false;
                    lkbDispReason.Visible = false;
                    gvSP.DataBind();
                }
            }
        }
    }
}